#include <stdio.h>
#include <stdbool.h>

/* 返回第courseIdx门课的平均成绩 */
float avgCourse(float (*pScore)[5], int studentNum, int courseIdx) {
    float avg = 0.0;
    for (int i = 0; i < studentNum; i++) 
        avg += pScore[i][courseIdx];
    return avg / studentNum;
}

/* 返回某一学生的平均成绩 */
float avgStudent(float *pStuScore, int courseNum) {
    float avg = 0.0;
    for (int i = 0; i < courseNum; ++i) 
        avg += pStuScore[i];
    return avg / courseNum;
}

/* 返回某一学生的最低成绩 */
float lowbdStudent(float * pStuScore, int courseNum) {
    float lowest = pStuScore[0];
    for (int i = 1; i < courseNum; i++)
        if (pStuScore[i] < lowest)
            lowest = pStuScore[i];
    return lowest;
}

/* 返回某一学生的不及格成绩个数 */
int failNumStudent(float * pStuScore, int courseNum) {
    int num = 0;
    for (int i = 0; i < courseNum; i++)
        if (pStuScore[i] < 60)
            num++;
    return num;
}

/* 输出第studentIdx个学生的成绩 */
void priScore(float (*pScore)[5], int studentIdx) {
    printf("学号为%d的学生所有成绩为: ", studentIdx);
    for (int i = 0; i < 5; i++) 
        printf("%.2f ", pScore[studentIdx][i]);
    printf("\n");
}

// (1)
float aveFirstCourse(float (*pScore)[5], int studentNum) {
    return avgCourse(pScore, studentNum, 0);
}

// (2)
void findTwoFailStudent(float (*pScore)[5], int studentNum, int courseNum) {
    for (int i = 0; i < studentNum; i++) {
        if (failNumStudent(pScore[i], courseNum) > 2) {
            // 输出学号
            printf("有两门以上课程不及格的学生的学号为: %d\n", i);
            // 输出全部课程成绩
            priScore(pScore, i);
            // 输出平均成绩
            float avg = avgStudent(pScore[i], courseNum);
            printf("学号为%d的平均成绩为: %.2f\n", i, avg);
        }
    }
}

// (3)
void findAvgOverNinetyandOverEightyFive(float (*pScore)[5], int studentNum, int courseNum) {
   
    for (int i = 0; i < studentNum; i++) {
        // 先找平均成绩在90分以上
        if (avgStudent(pScore[i], courseNum) > 90) 
            printf("平均在90分以上的学生学号有: %d\n", i);

        // 找全部课程成绩在85分以上的学生
        bool flag = true;
        for (int j = 0; j < courseNum; j++) {
            if (pScore[i][j] <= 85) {
                flag = false;
                break;
            }
        }
        if (flag)
            printf("全部课程成绩在85分以上的学生学号有: %d\n", i);
    }
    
}

#define STUDENT_NUM 4
#define COURSE_NUM  5

int main() {
    // 学生成绩数据
    float scores[STUDENT_NUM][COURSE_NUM] = {
        {85, 70, 90, 88, 95},
        {78, 60, 55, 80, 76},
        {95, 90, 92, 89, 96},
        {60, 55, 45, 67, 55}
    };
    float avgFirst = aveFirstCourse(scores, STUDENT_NUM);
    printf("第一门课程的平均分为: %.2f\n", avgFirst);

    printf("\n");
    findTwoFailStudent(scores, STUDENT_NUM, COURSE_NUM);
    printf("\n");

    findAvgOverNinetyandOverEightyFive(scores, STUDENT_NUM, COURSE_NUM);

    return 0;
}